//
//  WSVisitView.swift
//  微博
//
//  Created by 武莎莎 on 15/9/6.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class WSVisitView: UIView {
    
    
    // 手码开发会用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    // SB开发会被调用
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setUpUI()
    }
    // MARK - 设置visitView的信息
    func setUpVisitInfo(imageName:String?, txt:String){
        messageLbl.text = txt
        
        // 判断是否是首页，如果有传一张图片进来就不是首页
        // if let : imgName 为非空
        if let imgName = imageName {
            iconView.image = UIImage(named: imgName)
            houseIconView.hidden = true
            // 把遮罩视图放到最后面
            sendSubviewToBack(maskIconView)
        }else{
            // 是首页就开始动画
            startAnimation()
        }
    }
    private func startAnimation(){
        let anim:CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 3
        anim.repeatCount = MAXFLOAT // 无限重复
        anim.removedOnCompletion = false // 当完成是否删除动画
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var houseIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon-1"))
    private lazy var messageLbl:UILabel = {
        let lbl = UILabel()
        
        lbl.text = "关注一些人，回这里看看有什么惊喜"
        lbl.numberOfLines = 0
        lbl.font = UIFont.systemFontOfSize(14)
        lbl.textColor = UIColor.darkGrayColor()
        lbl.textAlignment = NSTextAlignment.Center
        
        return lbl
        }()
    // 遮罩层
    private lazy var maskIconView:UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return imageView
        }()
    lazy var loginBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.titleLabel?.textAlignment = NSTextAlignment.Center
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return btn
        }()
    lazy var registerBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.titleLabel?.textAlignment = NSTextAlignment.Center
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return btn
        
        }()
    
    private func setUpUI(){
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(messageLbl)
        addSubview(loginBtn)
        addSubview(registerBtn)
        // 添加约束:代码设置自动布局的时候，最好有一个固定的参照物
        // 房子外面的圆环
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item:iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item:iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        // 房子
        houseIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: houseIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        // 文本
        messageLbl.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLbl, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLbl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem:iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        // 文本框换行：numberOfLine== 0 && 给文本框一个固定的宽度
        addConstraint( NSLayoutConstraint(item: messageLbl, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        // 注册按钮
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 登陆按钮
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 10))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //  遮罩视图 - VFL 可视化布局语言
        /**
        H: 水平方向
        V: 垂直方向
        | 边界
        [] 控件
        矩阵：
        metrics: 极少用
        views: [key, VFL 中[] 括起的名称, value: 控件] -> 控件和 VFL 的映射
        */
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        let cons = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v" : maskIconView])
        addConstraints(cons)
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-(-35)-[regBtn]", options: [], metrics: nil, views: ["v":maskIconView, "regBtn": registerBtn]))
        
        // 设置背景颜色 - 灰度图 r = g = b
        // 提高程序效率的一个细节，如果能够用颜色表示，就不要用图片
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    
}
